﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule.MasterData
{
    public class DisCountManager:BusinessLogic
    {
        public DISCOUNTS Load(int ID)
        {
            return (DISCOUNTS)ObjectRow.Load(typeof(DISCOUNTS), string.Format("this.ID={0}", ID));
        }

        public DISCOUNTS LoadWithDomainID(string SAPDISCCODE, decimal DomainID,decimal ID)
        {
            return (DISCOUNTS)ObjectRow.Load(typeof(DISCOUNTS), string.Format("this.SAPDISCCODE='{0}' AND this.DOMAINID='{1}' AND this.ID<>'{2}'", SAPDISCCODE, DomainID,ID));
        }

        //绑定判断是否有效
        public ObjectTable GetVALIDYes(int Valid, decimal domainid)
        {
            StringBuilder sb = new StringBuilder();
            if (Valid >= 0)
            {
                sb.AppendFormat("this.VALID='{0}'", Valid);
            }
            if (domainid > 0)
            {
                sb.AppendFormat("and this.DOMAINID='{0}'", domainid);
            }
            return new ObjectTable(typeof(DISCOUNTS), sb.ToString().TrimStart('a', 'n', 'd'));
        }

        public static DataSet GetDiscountForBind(decimal domaind)
        {
            return Jumpton.Data.SqlData.OpenSP("sp_GetDiscountForBind",domaind);
        }

        public static DataSet GetDiscountForBindForPromotion(decimal domaind)
        {
            return Jumpton.Data.SqlData.OpenSP("sp_GetDiscountForBindForPromotion", domaind);
        }

        public static ObjectTable GetDiscount(decimal domainid)
        {
            return new ObjectTable(typeof(DISCOUNTS), "this.DOMAINID='" + domainid + "'");
        }
        /// <summary>
        /// 创建折扣
        /// </summary>
        /// <param name="description">描述</param>
        /// <param name="type">类型</param>
        /// <param name="vaild">是否有效</param>
        /// <param name="checkLevels"></param>
        /// <param name="levelsUOM"></param>
        /// <param name="level1"></param>
        /// <param name="disCount1"></param>
        /// <param name="level2"></param>
        /// <param name="disCount2"></param>
        /// <param name="level3"></param>
        /// <param name="disCount3"></param>
        /// <param name="level4"></param>
        /// <param name="disCount4"></param>
        /// <param name="sapDiscCode"></param>
        /// <param name="domainID"></param>
        /// <param name="createBy"></param>
        /// <returns></returns>
        public static decimal CreateDisCount(string description,
                                             decimal type,
                                             bool vaild,
                                             string checkLevels,
                                             string levelsUOM,
                                             decimal level1,
                                             decimal disCount1,
                                             decimal level2,
                                             decimal disCount2,
                                             decimal level3,
                                             decimal disCount3,
                                             decimal level4,
                                             decimal disCount4,
                                             string sapDiscCode,
                                             decimal domainID,
                                             string createBy
                                            )
        {
            DISCOUNTS discount = new DISCOUNTS();
           
            discount.DESCRIPTION = description;
            discount.TYPE = type;
            discount.ACTIVE = vaild;
            discount.CHECKLEVELS = checkLevels;
            discount.LEVELSUOM = levelsUOM;
            discount.LEVEL1 = level1;
            discount.DISCOUNT1 = disCount1;
            discount.LEVEL2 = level2;
            discount.DISCOUNT2 = disCount2;
            discount.LEVEL3 = level3;
            discount.DISCOUNT3 = disCount3;
            discount.LEVEL4 = level4;
            discount.DISCOUNT4 = disCount4;
            discount.SAPDISCCODE = sapDiscCode;
            discount.DOMAINID = domainID;
            discount.LAST_UPDATER = createBy;
            discount.LAST_UPDATE_TIME = long.Parse(BusinessLogic.GetDateTimeString(DateTime.Now));

            try
            {
                discount.Save();
                return discount.ID;
            }
            catch (Exception err)
            {
                throw new Exception(err.Message);
               
            }
        }

        public bool Save(DISCOUNTS discount)
        {
            try
            {
                //判断是否已经存在此代码
                if (LoadWithDomainID(discount.SAPDISCCODE, discount.DOMAINID,discount.ID) != null)
                {
                    throw new Exception("代码已存在");
                }
               
                
                discount.Save();
               
                return true;
            }
            catch (Exception ex)
            {
                this.message = ex.Message;
                throw ex;
            }
        }
    }
}
